package org.springbus.ff.util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

public class ImageUtils {

    public static void createEmptyPic(int w, int h, String color, String toPath) {
        try {
            BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2 = bufferedImage.createGraphics();
            g2.setBackground(parseColor(color));
            g2.fillRect(0, 0, w, h);
            g2.dispose();
            ImageIO.write(bufferedImage, "png", new File(toPath));
        } catch (Exception ex) {

        }
    }

    private static Color parseColor(String color) {
        if (color.startsWith("#")) {
            int r = Integer.parseInt(color.substring(1, 1 + 2));
            int g = Integer.parseInt(color.substring(3, 3 + 2));
            int b = Integer.parseInt(color.substring(3 + 2));
            return new Color(r, g, b);
        } else {
            return Color.getColor(color);
        }
    }
}
